
local Wheel = Wheel

local spells = Wheel.spells

function Wheel:InitializeAnnounce(name)
	if not self:IsValidSpell(name) then
		return
	end

	local db = self.db.profile.announces

--	db[spellid] = copyTable(db[spelid], db.general)

--	for k, v in pairs(db.general) do
--		if not db[spellid][k] then
--			db[spellid][k] = v
--		end
--	end

	if not db.rules[name] then
		local t = {}

		t.enable = true
		t.usegeneralenable = true
		t.enablezone = {}
		t.enablezone.world = db.general.enablezone.world
		t.enablezone.bg = db.general.enablezone.bg
		t.enablezone.arena = db.general.enablezone.arena
		t.enablezone.partyinstance = db.general.enablezone.partyinstance
		t.enablezone.raidinstance = db.general.enablezone.raidinstance
		t.usegeneralchannels = true
		t.channels = {}
		for k, v in pairs(db.general.channels) do
			if v then
				t.channels[k] = true
			end
		end
		t.messages = {}
		t.messages.start = ">>> $s up <<<"
		t.messages.finish = ">>> $s ends <<<"
		t.messages.whisper = ">>> $s on you <<<"

		db.rules[name] = t
	end

	self:AddIDs(name)

	self:AddAnnounceOptions(name, name)
end

function Wheel:DeleteAnnounce(name)
	self.db.profile.announces.rules[name] = nil

	self:DeleteIDs(name)

	self:DeleteAnnounceOptions(name, name)
end

function Wheel:GetChatChannelIndex(name)
	local index

	local channel_list = { GetChannelList() }
	for i = 2, #channel_list, 2 do
		if channel_list[i] == name then
			-- FIXME: should I just return here?
			index = channel_list[i - 1]
			break
		end
	end

	return index
end

function Wheel:IsServerChannel(channel)
	local server_channels = { EnumerateServerChannels() }

	local found
	for k, v in pairs(server_channels) do
		if v == channel then
			-- FIXME: should I just return here?
			found = true
			break
		end
	end

	return found
end

function Wheel:GetCustomChannelList()
	local all_channels = { GetChannelList() }
	local channels = {}

	local name
	for i = 2, #all_channels, 2 do
		name = all_channels[i]
		if not self:IsServerChannel(name) then
			channels[name] = name
		end
	end

	return channels
end

function Wheel:CreateAnnounce(name)
--[[	local t = {}

	t.enablerule = "general"
	t.enablezone_world = nil
	t.enablezone_bg = nil
	t.enablezone_arena = nil
	t.enablezone_partyinstance = nil
	t.enablezone_raidinstance = true

	self.db.profile.announces.rules[name] = t
	]]
end

function Wheel:AddAnnounceStartToSpell(name, announce_start)
	self.db.profile.announces.rules[name].announce_start = announce_start
end

function Wheel:AddAnnounceEndToSpell(name, announce_end)
	self.db.profile.announces.rules[name].announce_end = announce_end
end

function Wheel:AddAnnounceChannelsToRule(name, announce_channel0, announce_channel1)
	self.db.profile.announces.rules[name].announce_channel0 = announce_channel0
	self.db.profile.announces.rules[name].announce_channel1 = announce_channel1
end

function Wheel:AddIDs(name)
	for class, t in pairs(spells) do
		for id in pairs(t) do
			if GetSpellInfo(id) == name then
				self.db.profile.announces.map[tostring(id)] = name
			end
		end
	end
end

function Wheel:DeleteIDs(name)
	for k, v in pairs(self.db.profile.announces.map) do
		if v == name then
			self.db.profile.announces.map[k] = nil
		end
	end
end


